package com.roger.springcloudGreenwich.service.impl;

import com.roger.springcloudGreenwich.bean.Commodity;
import com.roger.springcloudGreenwich.bean.Epidemic;
import com.roger.springcloudGreenwich.repository.EpidemicRepository;
import com.roger.springcloudGreenwich.service.EpidemicService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2020/2/26.
 */
@Service
@Slf4j
public class EpidemicServiceImpl implements EpidemicService {
    @Autowired
    private EpidemicRepository epidemicRepository;

    @Override
    public void save(Epidemic epidemic) {
        epidemicRepository.save(epidemic);
    }

    @Override
    public void delete(Epidemic epidemic) {
        List<Epidemic> list = new ArrayList<>();

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchPhraseQuery("uuid", epidemic.getUuid()))
                .build();

        Iterable<Epidemic> iterable = epidemicRepository.search(searchQuery);
        iterable.forEach(e->list.add(e));
        epidemic = list.get(0);
        epidemicRepository.delete(epidemic);
    }

    @Override
    public void update(Epidemic epidemic) {

    }
}
